package com.it.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.it.entity.user.AiStaffUser;
import com.it.mapper.AiStaffUserMapper;
import com.it.service.AiStaffUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.R;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 * 公司工作人员信息表（含职位信息，供业务表关联） 服务实现类
 * </p>
 *
 * @author lutao
 * @since 2025年09月17日
 */
@Service
public class AiStaffUserServiceImpl extends ServiceImpl<AiStaffUserMapper, AiStaffUser> implements AiStaffUserService {

    @Autowired
    private AiStaffUserMapper aiStaffUserMapper;

    @Override
    public R list(Integer pageNum, Integer pageSize, String workNo, String realName, String positionCode) {
        PageHelper.startPage(pageNum,pageSize);
        List<AiStaffUser> list = aiStaffUserMapper.getList(workNo,realName,positionCode);
        PageInfo<AiStaffUser> pageInfo = new PageInfo<>(list);
        return R.Success("员工用户查询成功",pageInfo);
    }

    @Override
    public void exportExcel(int pageNum, int pageSize, String workNo, String realName, String positionCode, HttpServletResponse response) {
        PageHelper.startPage(pageNum,pageSize);
        List<AiStaffUser> list = aiStaffUserMapper.getList(workNo,realName,positionCode);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("员工用户报表", "第一项"), AiStaffUser.class, list);
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("UTF-8");
            String fileName = URLEncoder.encode("员工用户报表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            workbook.write(response.getOutputStream());
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public R workers(String positionCode) {
        LambdaQueryWrapper<AiStaffUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AiStaffUser::getPositionCode,positionCode);
        queryWrapper.eq(AiStaffUser::getStatus,1);
        List<AiStaffUser> list = aiStaffUserMapper.selectList(queryWrapper);

        return R.Success("查询成功,已返回符合条件的员工名称列表",list);
    }
}
